package com.zxs.read;

import java.io.*;

public class ReadFileInFolder {
    public static String readContent(String path,String filename) throws IOException {
        File folder = new File(path);
        StringBuilder contentBuilder = new StringBuilder();
        if (folder.exists()) {
            if (folder.isDirectory()) {
                File[] files = folder.listFiles(); // 获取文件夹内的所有文件
                for (File file : files) {
                    if (!file.isDirectory()) { // 只处理非目录文件
                        if (file.getName().equals(filename+".zxs")) { // 按名称查找特定文件
                            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                            String line;
                            while ((line = reader.readLine()) != null) {
                                 contentBuilder.append(line).append("\n");
                            }
                            reader.close();
                        }
                    }
                }
                return contentBuilder
                        .toString();
            } else {
                throw new IllegalArgumentException("The path is not a directory.");
            }
        } else {
            throw new FileNotFoundException("The specified folder does not exist.");
        }
    }
}
